$(document).ready(
	function(){
		
	}
)

function initPageModifyProfile()
{
	var babyGender = 0;

	$("#header").html("<p id='btnHeaderBack' class='btnHeaderBack btnHeaderLeft'></p>아가 수정");
	$("#btnPhotoProfile").css("left", ($(window).width()/2 - 39)+"px" );
	$("#user_join_bg").css("height",$(window).height()+100);

	new Quickly('btnHeaderBack', function(e){
		$.mobile.changePage("../main_profile.html",{transition:'none'});
	});
	new Quickly('aga_profile_frame', function(e){
		selectPicture();
	});
	new Quickly('btnModifyAga', function(e){
		checkModifyBaby();
		 modifyRoomToServer( 'btnPhotoProfile', user.id, 0, $.trim($("#babyName").val()), $.trim($("#babyNick").val()), $("#babyBirthday").val(), babyGender );
	});
	new Quickly('join_man', function(e){
		$("#join_man").css("background-image","url(../images/user/btn_male_on.png)");
		$("#join_woman").css("background-image","url(../images/user/btn_female.png)");
		babyGender=1;
	});
	new Quickly('join_woman', function(e){
		$("#join_man").css("background-image","url(../images/user/btn_male.png)");
		$("#join_woman").css("background-image","url(../images/user/btn_female_on.png)");
		babyGender=2;
	});
	new Quickly('show', function(e){
		$('#babyBirthday').scroller('show'); 
	});

    $('#babyBirthday').scroller({
        preset: 'date',
        minDate: new Date(2010, 1, 1),
        theme: 'default',
        display: 'modal',
        mode: 'mixed'
    });
}

function initPageProfileFriendlist()
{
	nowURL = "profile/friendlist.html";

	$("#header").html("<p id='btnHeaderBack' class='btnHeaderBack btnHeaderLeft'></p>친구<p id='btnHeaderModify' class='btnHeaderModify btnHeaderRight'></p>");
	
	new Quickly('btnHeaderBack', function(e){
		$.mobile.changePage("../main_profile.html",{transition:'none'});
	});
	new Quickly('btnHeaderModify', function(e){
		modifyFriendList();
	});
	new Quickly('btnAddFriend', function(e){
		$.mobile.changePage("invite_aga.html", {transition : 'none'});
	});

	requestFriendListToServer( user.id, user.mainbaby_info.babyid );
}

function initPageProfileInviteAga()
{
	nowURL = "profile/invite_aga.html";

	$("#header").html("친구추가<p id='btnHeaderClose' class='btnHeaderClose btnHeaderRight'></p>");
	new Quickly('btnHeaderClose', function(e){
		$.mobile.changePage("friendlist.html",{transition:'flip'})
	});
	new Quickly('btnSearch', function(e){
		if( $("#babyNick").val() != "" ){
			requestSearchBabyWithBabyIdToServer(user.mainbaby_info.babyid, $.trim($("#babyNick").val()) );
		}
	});
}

function initPageProfileFamilylist()
{
	nowURL = "profile/familylist.html";

	$("#header").html("<p id='btnHeaderBack' class='btnHeaderBack btnHeaderLeft'></p>가족<p id='btnHeaderModify' class='btnHeaderModify btnHeaderRight'></p>");

	requestFamilyListToServer( user.id, user.mainbaby_info.babyid );

	new Quickly('btnHeaderBack', function(e){
		$.mobile.changePage("../main_profile.html",{transition:'slide'});
	});
	new Quickly('btnHeaderModify', function(e){
		modifyFamilyList();
	});
	new Quickly('btnAddFamily', function(e){
		$.mobile.changePage("invite_family.html", {transition : 'none'});
	});
}

function initPageProfileInviteFamily()
{
	nowURL = "profile/invite_family.html";

	$("#header").html("가족추가</p><p id='btnHeaderClose' class='btnHeaderClose btnHeaderRight'></p>");
	new Quickly('btnHeaderClose', function(e){
		$.mobile.changePage("familylist.html",{transition:'flip'})
	});
	new Quickly('btnSearch', function(e){
		reqeustSearchFamilyToServer( user.mainbaby_info.babyid, $("#userEmail").val() );
	});
}

function initPageProfileAgalist()
{
	nowURL = "profile/agalist.html";

	$("#header").html("<p id='btnHeaderBack' class='btnHeaderBack btnHeaderLeft'></p>아가들");

	refreshAgaList( user.babyInfo );

	new Quickly('btnHeaderBack', function(e){
		$.mobile.changePage("../main_profile.html",{transition:'slide'})
	});
	new Quickly('btnCreateAga', function(e){
		$.mobile.changePage("create_room.html", {transition : 'none'});
	});
}

function initPageCreateAga()
{
	$("#header").html("<p id='btnHeaderBack' class='btnHeaderBack btnHeaderLeft'></p>아가방 만들기");
	$("#btnPhotoProfile").css("left", ($(window).width()/2 - 39)+"px" );
	new Quickly('btnHeaderBack', function(e){
		$.mobile.changePage("../main_profile.html",{transition:'slide'});
	});
}

//수정 완료
function refreshFriendList( friendList, start )
{
	$("#profile_friendList").html("");
	$.each( friendList, function(i, item){
		var temp = "<li id='friendList'>"
				+ "<div class='list_li'>"
				+ "<div id='list_frame_bg"+(start+i)+"' class='list_frame_bg'>"
				+ "<img id='list_gender"+(start+i)+"' class='list_gender' src='../../images/profile/gender_male.png'>"
				+ "<p id='list_nick"+(start+i)+"' class='list_nick'>"+item.nick+"</p>"
				+ "<p id='list_babybirthday"+(start+i)+"' class='list_babybirthday'>생일 "+item.birthday.substr(0,10)+"</p>"
				+ "<p id='list_babyfrombirth"+(start+i)+"' class='list_babyfrombirth'>태어난지 "+countDay(item.birthday)+"일 되는 날</p>"
				+ "<p id='list_btn_delete' class='list_btn_delete'></p>"
				+ "<p id='list_resemble_bg"+(start+i)+"' class='list_resemble_bg'><p id='list_resemble_text"+(start+i)+"' class='list_resemble_text'>"+checkSimilarity(item.similarity)+"%"
				+ "<input id='list_friendid"+(start+i)+"' style='display:none !important;' value='"+item.babyid+"'></p>"
				+ "</div></div></li>";

		$("#profile_friendList").append(temp);
		$("#list_frame_bg"+(start+i)).css("background-image", "url('../../images/list/sum_middle_baby.png'), "+ urlProfilePhoto(item.babyid, item.pf_name,"medium"));		
		$("#list_gender"+(start+i)).attr("src",checkGender(2, item.gender));
	});

	$("#profile_friendList #friendList").click(function(){
		if(modifyInfo.modify == true )
			return;
		
		var idx = $("#friendList").index($(this));
		profileInfo.babyId = $("#list_friendid"+idx).val();

		$.mobile.changePage("../main_profile.html",{transition:'none'});
	})
	
	for (x in myScroll) 
		myScroll[x].refresh(); 	
}

//수정 완료
function refreshFamilyList( friendList, start )
{
	debug = friendList;
	$("#profile_familyList").html("");
	$.each( friendList, function(i, item){
		var temp = "<li>"
				+ "<div class='list_li'>"
				+ "<div id='list_frame_bg"+(start+i)+"' class='list_frame_bg'>"
				+ "<img id='list_gender"+(start+i)+"' class='list_gender' src='../../images/profile/gender_male.png'>"
				+ "<p id='list_nick"+(start+i)+"' class='list_nick'>"+item.name+"</p>"
				+ "<p id='list_btn_delete' class='list_btn_delete' style='top:-35px;'></p>"
				+ "<input id='list_familyid"+(start+i)+"' style='display:none !important;' value='"+item.userid+"'></p>"
				+ "</div></div></li>";
		
		$("#profile_familyList").append(temp);
		$("#list_frame_bg"+(start+i)).css("background-image", "url('../../images/list/sum_middle_baby.png'), "+ urlProfilePhoto(user.mainbaby_info.babyid, user.mainbaby_info.pf_name,"medium"));		
		$("#list_gender"+(start+i)).attr("src",checkGender(2, item.gender));
	});

	for (x in myScroll) 
		myScroll[x].refresh(); 	
}



//수정 완료
function refreshAgaList( agaList )
{
	$("#profile_agaList").html("");
	$.each( agaList, function(i, item){
		var temp = "<li id='agaList'>"
				+ "<div class='list_li'>"
				+ "<div id='list_frame_bg"+i+"' class='list_frame_bg'>"
				+ "<img id='list_gender"+i+"' class='list_gender' src='../../images/profile/gender_male.png'>"
				+ "<p id='list_nick"+i+"' class='list_nick'>"+item.nick+"</p>"
				+ "<p id='list_babybirthday"+i+"' class='list_babybirthday'>생일 "+item.birthday.substr(0,10)+"</p>"
				+ "<p id='list_babyfrombirth"+i+"' class='list_babyfrombirth'>태어난지 "+countDay(item.birthday)+"일 되는 날</p>"
				+ "<input id='list_agaid"+i+"' style='display:none !important;' value='"+item.babyid+"'></p>"
				+ "</div></div></li>";

		$("#profile_agaList").append(temp);
		$("#list_frame_bg"+i).css("background-image", "url('../../images/list/sum_middle_baby.png'), "+ urlProfilePhoto(item.babyid, item.pf_name,"medium"));		
		$("#list_gender"+i).attr("src",checkGender(2, item.gender));
	});

	var temp = "<p class='list_bottom'>선택하면 메인 아가가 됩니다</p><p id='btnCreateAga' class='btnCreateAga'></p>";
	$("#profile_agaList").append(temp);
		
	$("#profile_agaList .list_li").click(function(){
		var idx = $(".list_li").index($(this));
		if( $("#list_agaid"+idx).val() != user.mainbaby_info.babyid )
		{
			requestChangeMainBabyToServer(user.id, user.mainbaby_info.babyid, $("#list_agaid"+idx).val() );
		}
	})

	for (x in myScroll) 
		myScroll[x].refresh();
}

//수정 완료
function refreshSearchBabyList( friendList, start )
{
	$("#invite_agaList").html("");
	$.each( friendList, function(i, item){
		var temp = "<li>"
				+ "<div class='list_li'>"
				+ "<div id='list_frame_bg"+(start+i)+"' class='list_frame_bg'>"
				+ "<img id='list_gender"+(start+i)+"' class='list_gender' src='../../images/profile/gender_male.png'>"
				+ "<p id='list_nick"+(start+i)+"' class='list_nick'>"+item.nick+"</p>"
				+ "<p id='list_babybirthday"+(start+i)+"' class='list_babybirthday'>생일 "+item.birthday.substr(0,10)+"</p>"
				+ "<p id='list_babyfrombirth"+(start+i)+"' class='list_babyfrombirth'>태어난지 "+countDay(item.birthday)+"일 되는 날</p>"
				+ "<p id='list_btn_invite' class='list_btn_invite'></p>"
				+ "<input id='searchBabyId"+(start+i)+"' style='display:none;' value='"+item.babyid+"'>"
				+ "</div></div></li>";
		
		$("#invite_agaList").append(temp);
		$("#list_frame_bg"+(start+i)).css("background-image", "url('../../images/list/sum_middle_baby.png'), "+ urlProfilePhoto(item.babyid, item.pf_name,"medium"));		
		$("#list_gender"+(start+i)).attr("src",checkGender(2, item.gender));
		
		if( item.already_request == 1 )
			$("#list_btn_invite").eq(i).css("background-image",'url(../../images/list/btn_request_ing.png)');
	});

	var temp = "<p class='list_bottom'>";
	$("#invite_agaList").append(temp);

	$("#invite_agaList #list_btn_invite").click(function(){
		var idx = $(".list_btn_invite").index($(this));
		alert( user.babyId );
		requestFriendToServer( user.mainbaby_info.babyid, $("#searchBabyId"+idx).val(), idx );
	})
	for (x in myScroll) 
		myScroll[x].refresh(); 	
}


//수정 완료
function refreshSearchFamilyList( familyList, start )
{
	$("#invite_familyList").html("");
	$.each( familyList, function(i, item){
		var temp = "<li>"
				+ "<div class='list_li'>"
				+ "<div id='list_frame_bg"+(start+i)+"' class='list_frame_bg'>"
				+ "<img id='list_gender"+(start+i)+"' class='list_gender' src='../../images/profile/gender_male.png'>"
				+ "<p id='list_nick"+(start+i)+"' class='list_nick'>"+item.name+"</p>"
				+ "<p id='list_btn_invite_family' class='list_btn_invite_family'></p>"
				+ "<input id='searchUserId"+(start+i)+"' style='display:none;' value='"+item.userid+"'>"
				+ "</div></div></li>";
		
		$("#invite_familyList").append(temp);
		$("#list_frame_bg"+(start+i)).css("background-image", "url('../../images/list/sum_middle_baby.png'), "+ urlProfilePhoto(item.babyid, item.pf_name,"medium"));		
		$("#list_gender"+(start+i)).attr("src",checkGender(2, item.gender));

		if( item.already_request == 1 )
			$("#list_btn_invite_family").eq(i).css("background-image",'url(../../images/list/btn_request_ing.png)');
	});

	var temp = "<p class='list_bottom'>";
	$("#invite_familyList").append(temp);

	$("#invite_familyList #list_btn_invite_family").click(function(){
		var idx = $(".list_btn_invite_family").index($(this));
		InviteFamilyToServer( user.id, $("#searchUserId"+idx).val(), idx );
	})

	for (x in myScroll) 
		myScroll[x].refresh(); 	
}

// 완성
function modifyFriendList()
{
	liCount = $("#profile_friendList > li").length;
	for( var i=0; i<liCount; i++ )
	{
		$("#list_resemble_text"+i).css("display", "none");
		$("#list_resemble_bg"+i).css("display", "none");
		$(".list_btn_delete").css("display", "block");
	}

	$("#profile_friendList #list_btn_delete").click(function(e){
		modifyInfo.modify = true;
		var idx = $(".list_btn_delete").index($(this));
		requestDeleteFriendToServer( user.id, user.mainbaby_info.babyid, $("#list_friendid"+idx).val() );
	})
}

//수정 완료
function modifyFamilyList()
{
	liCount = $("#profile_familyList > li").length;
	for( var i=0; i<liCount; i++ )
	{
		$("#list_resemble_text"+i).css("display", "none");
		$("#list_resemble_bg"+i).css("display", "none");
		$("#list_btn_delete"+i).css("display", "block");
	}

	$("#profile_familyList #list_btn_delete").click(function(){
		modifyInfo.modify = true;
		var idx = $(".list_btn_delete").index($(this));
		requestDeleteFamilyToServer( user.id, user.mainbaby_info.babyid, $("#list_familyid"+idx).val() );
	})
}

function checkModifyBaby(){
	if( $.trim($("#babyName").val()) == '' ){
		alert("아기 이름을 입력해 주세요.");
        $("#babyName").focus();
        return;
    }
    if( $.trim($("#babyNick").val()) == '' ){
        alert("아기 별명을 입력해 주세요.");
        $("#babyNick").focus();
        return;
    }
    if( $.trim($("#babyBirthday").val()) == '' ){
    	alert("아기 생일을 입력해 주세요.");
        return;
    }
	if (babyGender == 0) {
		alert("성별을 선택해 주세요.");
		return;
	}
}

function changeLocalProfile( new_babyinfo )
{
	user.mainbaby_info = new_babyinfo;
	
	profileInfo.init();
	postInfo.init();
	snsPostInfo.init();
	$.mobile.changePage("../main_profile.html",{transition:'slide'});
}

